/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.Speaking;
import Controller.SpeakingDAO;
import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author ngoc
 */
public class Faddspeaking extends javax.swing.JFrame {

    /**
     * Creates new form Faddspeaking
     */
    SpeakingDAO spDAO = new SpeakingDAO();
    String urlfileSpeaking ;
    String urlfileTime;
    public Faddspeaking() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel51 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel53 = new javax.swing.JLabel();
        txturlspeaking = new javax.swing.JTextField();
        btnchoospeaking = new javax.swing.JButton();
        btnadd = new javax.swing.JButton();
        btnremove = new javax.swing.JButton();
        jLabel55 = new javax.swing.JLabel();
        txturltime = new javax.swing.JTextField();
        jLabel54 = new javax.swing.JLabel();
        btnchoosetime = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel51.setText("Name");

        jLabel53.setText("url question");

        btnchoospeaking.setText("choose");
        btnchoospeaking.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnchoospeakingActionPerformed(evt);
            }
        });

        btnadd.setText("add");
        btnadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        });

        btnremove.setText("remove");
        btnremove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnremoveActionPerformed(evt);
            }
        });

        jLabel55.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel55.setText("ADD SPEAKING");

        jLabel54.setText("url Time");

        btnchoosetime.setText("choose");
        btnchoosetime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnchoosetimeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel55, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel53)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txturlspeaking, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(btnchoospeaking, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel51)
                                    .addGap(34, 34, 34)
                                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel54)
                                .addGap(30, 30, 30)
                                .addComponent(txturltime, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnchoosetime, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(95, 95, 95)
                        .addComponent(btnadd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnremove)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel55, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel51)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel53)
                    .addComponent(txturlspeaking, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnchoospeaking))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel54)
                    .addComponent(txturltime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnchoosetime))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnadd)
                    .addComponent(btnremove))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(11, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaddActionPerformed
        // TODO add your handling code here:
        if (txtName.getText().equals("") || txturlspeaking.getText().equals("")  || txturltime.getText().equals("") ) {
           spDAO.infoBox("erro input", "messages");
        }
        else{
        int id = spDAO.getMaxId()+1;
        Speaking obj = new Speaking();
        obj.setSpeakingID(id);
        obj.setSpeakingname(txtName.getText());
        obj.setUrlHistory("historyspeaking"+id+".txt");
        obj.setUrlSpeaking(txturlspeaking.getText());
        obj.setUrlTime(txturltime.getText());
        spDAO.add(obj);
        spDAO.infoBox("add file successfully", "messages");
        spDAO.CoppyFile(urlfileTime, txturltime.getText());
        spDAO.CoppyFile(urlfileSpeaking, txturlspeaking.getText());
        txtName.setText("");
        txturlspeaking.setText("");
        txturltime.setText("");
        }
    }//GEN-LAST:event_btnaddActionPerformed

    private void btnchoospeakingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnchoospeakingActionPerformed
        // TODO add your handling code here:
        JFileChooser filechooser = new JFileChooser();
        int returnValue = filechooser.showOpenDialog(null); 

        //Neu file duoc chon 
        if(returnValue==filechooser.APPROVE_OPTION) { 
            File selectedFile = filechooser.getSelectedFile(); 

        //in thu duong dan file 
        System.out.println("getPath : " + selectedFile.getPath()); 
        System.out.println("getAbsolutePath : " + selectedFile.getAbsolutePath()); 
        System.out.println("getName : " + selectedFile.getName()); 

        System.out.println("getName : " + filechooser.getCurrentDirectory().getPath()); 
        txturlspeaking.setText(selectedFile.getName());
        String st=selectedFile.getPath(); 
        System.out.print("lay duong dan: "+st); 
        urlfileSpeaking = st;
        }  
    }//GEN-LAST:event_btnchoospeakingActionPerformed

    private void btnremoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnremoveActionPerformed
        // TODO add your handling code here:
        txtName.setText("");
        txturlspeaking.setText("");
        txturltime.setText("");
    }//GEN-LAST:event_btnremoveActionPerformed

    private void btnchoosetimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnchoosetimeActionPerformed
        // TODO add your handling code here:
        JFileChooser filechooser = new JFileChooser();
        int returnValue = filechooser.showOpenDialog(null); 

        //Neu file duoc chon 
        if(returnValue==filechooser.APPROVE_OPTION) { 
            File selectedFile = filechooser.getSelectedFile(); 

        //in thu duong dan file 
        System.out.println("getPath : " + selectedFile.getPath()); 
        System.out.println("getAbsolutePath : " + selectedFile.getAbsolutePath()); 
        System.out.println("getName : " + selectedFile.getName()); 

        System.out.println("getName : " + filechooser.getCurrentDirectory().getPath()); 
        txturltime.setText(selectedFile.getName());
        String st=selectedFile.getPath(); 
        System.out.print("lay duong dan: "+st); 
        urlfileTime = st;
        }  
    }//GEN-LAST:event_btnchoosetimeActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Faddspeaking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Faddspeaking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Faddspeaking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Faddspeaking.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Faddspeaking().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnadd;
    private javax.swing.JButton btnchoosetime;
    private javax.swing.JButton btnchoospeaking;
    private javax.swing.JButton btnremove;
    private javax.swing.JLabel jLabel51;
    private javax.swing.JLabel jLabel53;
    private javax.swing.JLabel jLabel54;
    private javax.swing.JLabel jLabel55;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txturlspeaking;
    private javax.swing.JTextField txturltime;
    // End of variables declaration//GEN-END:variables
}
